{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.输入某年某月某日，判断这一天是这一年的第几天？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你输入的日期是 2020 年 10 月 1 日\n",
      "2020 年 10 月 1 日是 2020 年的第 275 天\n"
     ]
    }
   ],
   "source": [
    "list1 = []\n",
    "for i in range(3):\n",
    "    date = input('请输入你想查询的年月日按先后顺序依次输入，每次输入一个，一共三次：')\n",
    "    list1.append(date)\n",
    "\n",
    "# 将输入的日期分别赋值给 year, month, day\n",
    "year = int(list1[0])\n",
    "month = int(list1[1])\n",
    "day = int(list1[2])\n",
    "\n",
    "print(f'你输入的日期是 {year} 年 {month} 月 {day} 日')\n",
    "\n",
    "# 判断是否为闰年\n",
    "if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:\n",
    "    month_day = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "else:\n",
    "    month_day = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "\n",
    "# 检查月份是否在有效范围内\n",
    "if 1 <= month <= 12:\n",
    "    # 计算输入日期是该年的第几天\n",
    "    total_day = sum(month_day[:month - 1]) + day\n",
    "    print(f'{year} 年 {month} 月 {day} 日是 {year} 年的第 {total_day} 天')\n",
    "else:\n",
    "    print('输入的月份不在有效范围内')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.输出9x9乘法口诀表。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你输入的数字是 9，乘法表如下：\n",
      "1*1=1   \t \n",
      "2*1=2   \t2*2=4   \t \n",
      "3*1=3   \t3*2=6   \t3*3=9   \t \n",
      "4*1=4   \t4*2=8   \t4*3=12  \t4*4=16  \t \n",
      "5*1=5   \t5*2=10  \t5*3=15  \t5*4=20  \t5*5=25  \t \n",
      "6*1=6   \t6*2=12  \t6*3=18  \t6*4=24  \t6*5=30  \t6*6=36  \t \n",
      "7*1=7   \t7*2=14  \t7*3=21  \t7*4=28  \t7*5=35  \t7*6=42  \t7*7=49  \t \n",
      "8*1=8   \t8*2=16  \t8*3=24  \t8*4=32  \t8*5=40  \t8*6=48  \t8*7=56  \t8*8=64  \t \n",
      "9*1=9   \t9*2=18  \t9*3=27  \t9*4=36  \t9*5=45  \t9*6=54  \t9*7=63  \t9*8=72  \t9*9=81  \t \n"
     ]
    }
   ],
   "source": [
    "# 获取用户输入的正整数 n\n",
    "n = int(input('请输入一个正整数，程序将会生成你想要的乘法表：'))\n",
    "\n",
    "# 确保输入的 n 是一个正整数\n",
    "if n >= 1:\n",
    "    print(f'你输入的数字是 {n}，乘法表如下：')\n",
    "    \n",
    "    # 使用一个循环来生成 n*n 乘法表\n",
    "    for k in range(1, n * n + 1):\n",
    "        # 计算当前的行索引 i\n",
    "        i = (k - 1) // n + 1\n",
    "        # 计算当前的列索引 j\n",
    "        j = (k - 1) % n + 1\n",
    "        if j<=i:\n",
    "        # 打印乘法表达式，使用字符串格式化确保对齐\n",
    "            print(f\"{i}*{j}={i*j:<4}\", end=\"\\t\")\n",
    "        # 当列索引等于 n 时，打印换行符\n",
    "        if j == i:\n",
    "            print(' ')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.判断1000中有多少个素数，并输出所有素数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000中共有168个素数，素数为：[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n"
     ]
    }
   ],
   "source": [
    "n=int(input('请输入一个整数n，程序将查找n中共有多少个素数，并全部表示出来：'))\n",
    "primes=[2]#提前把2这个偶数放进去\n",
    "if n<=1:#小于1的数中没有素数\n",
    "    print('你输入的n中没有素数')\n",
    "elif n==2:#单独处理偶数2\n",
    "    print('2中共有1个素数，素数为：',primes)\n",
    "else:\n",
    "    count=1#直接把2这一个数算进去\n",
    "    for i in range(3,n+1,2):#除了2以外所有的素数都是奇数，从3开始步长为2，那每个数都是奇数\n",
    "        for j in range(3,i+1,2):#处理因子全是奇数的情况         \n",
    "            if i==j:#相等意味着内层循环结束，没有一个非本身的奇数是它的因子，那么这个i就是素数\n",
    "                primes.append(i)\n",
    "                count += 1\n",
    "                break\n",
    "            elif i%j==0:\n",
    "                break\n",
    "            else:\n",
    "                j+=2\n",
    "    print(f'{n}中共有{count}个素数，素数为：{primes}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4.输入一个任意正整数，输出分解质因数。例如：输入90,打印出90=2 * 3* 3 * 5。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "90=2*3*3*5\n"
     ]
    }
   ],
   "source": [
    "def prime_factors(n):\n",
    "    if n <= 1:\n",
    "        return \n",
    "    factors = []\n",
    "    # 处理2的因数\n",
    "    while n % 2 == 0:\n",
    "        factors.append(2)\n",
    "        n = n // 2\n",
    "    # 处理奇数因数\n",
    "    for i in range(3, int(n**0.5) + 1, 2):\n",
    "        while n % i == 0:\n",
    "            factors.append(i)\n",
    "            n = n // i\n",
    "    # 如果n是一个大于2的质数\n",
    "    if n > 2:\n",
    "        factors.append(n)\n",
    "    return factors\n",
    "\n",
    "# 获取用户输入的正整数\n",
    "n = int(input('请输入一个正整数：'))\n",
    "\n",
    "# 获取质因数分解结果\n",
    "factors = prime_factors(n)\n",
    "\n",
    "# 打印结果\n",
    "if n <= 1:\n",
    "    print(f\"{n}没有质因数\")\n",
    "else:\n",
    "    print(f\"{n}=\", end=\"\")\n",
    "    print(\"*\".join(map(str, factors)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5.利用条件运算符的嵌套来完成此题：学习成绩>=90分的同学用A表示，60-89分之间的用B表示，60分以下的用C表示。（要求有键盘输入）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "成绩等级为：A\n"
     ]
    }
   ],
   "source": [
    "# 获取用户输入的成绩\n",
    "score = int(input('请输入学习成绩：'))\n",
    "\n",
    "# 使用条件运算符的嵌套来判断成绩等级\n",
    "grade = 'A' if score >= 90 else ('B' if score >= 60 else 'C')\n",
    "\n",
    "# 打印成绩等级\n",
    "print(f'成绩等级为：{grade}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 6.输入一个不多于6位的正整数，要求：一、求它是几位数，二、逆序打印出各位数字。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "它是 6 位数\n",
      "逆序打印出各位数字：654321\n"
     ]
    }
   ],
   "source": [
    "# 获取用户输入的正整数\n",
    "num = input('请输入一个不多于6位的正整数：')\n",
    "\n",
    "# 确保输入的是一个正整数且不多于6位\n",
    "if num.isdigit() and 1 <= len(num) <= 6:\n",
    "    # 求它是几位数\n",
    "    length = len(num)\n",
    "    print(f'它是 {length} 位数')\n",
    "    \n",
    "    # 逆序打印出各位数字\n",
    "    reversed_num = num[::-1]\n",
    "    print(f'逆序打印出各位数字：{reversed_num}')\n",
    "else:\n",
    "    print('输入不合法，请输入一个不多于6位的正整数')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 7.输入一行字符串，分别统计出其中英文字母、空格、数字和其它字符的个数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "英文字母个数：3\n",
      "空格个数：1\n",
      "数字个数：3\n",
      "其它字符个数：3\n"
     ]
    }
   ],
   "source": [
    "# 获取用户输入的一行字符串\n",
    "input_str = input('请输入一行字符串：')\n",
    "\n",
    "# 初始化计数器\n",
    "letters = 0\n",
    "spaces = 0\n",
    "digits = 0\n",
    "others = 0\n",
    "\n",
    "# 遍历字符串中的每个字符并进行分类统计\n",
    "for char in input_str:\n",
    "    if char.isalpha():\n",
    "        letters += 1\n",
    "    elif char.isspace():\n",
    "        spaces += 1\n",
    "    elif char.isdigit():\n",
    "        digits += 1\n",
    "    else:\n",
    "        others += 1\n",
    "\n",
    "# 打印统计结果\n",
    "print(f'英文字母个数：{letters}')\n",
    "print(f'空格个数：{spaces}')\n",
    "print(f'数字个数：{digits}')\n",
    "print(f'其它字符个数：{others}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 8.求s=a+aa+aaa+aaaa+aa...a的值，其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加)，几个数相加有键盘控制。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s = 24690\n"
     ]
    }
   ],
   "source": [
    "# 获取用户输入的数字 a 和相加次数 n\n",
    "a = int(input('请输入一个数字 a：'))\n",
    "n = int(input('请输入相加的次数 n：'))\n",
    "\n",
    "# 初始化结果 s 和当前项 current_term\n",
    "s = 0\n",
    "current_term = 0\n",
    "\n",
    "# 计算 s 的值\n",
    "for i in range(n):\n",
    "    current_term = current_term * 10 + a\n",
    "    s += current_term\n",
    "\n",
    "# 打印结果\n",
    "print(f's = {s}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 9.一个数如果恰好等于它的因子之和，这个数就称为“完数”。例如6=1＋2＋3.编程，找出1000以内的所有完数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000以内的完数有： [6, 28, 496]\n"
     ]
    }
   ],
   "source": [
    "# 定义一个函数来判断一个数是否为完数\n",
    "def is_perfect_number(num):\n",
    "    # 初始化因子和\n",
    "    sum_of_factors = 0\n",
    "    # 找出所有因子并求和\n",
    "    for i in range(1, num):\n",
    "        if num % i == 0:\n",
    "            sum_of_factors += i\n",
    "    # 判断因子和是否等于该数\n",
    "    return sum_of_factors == num\n",
    "\n",
    "# 找出1000以内的所有完数\n",
    "perfect_numbers = []\n",
    "for num in range(1, 1001):\n",
    "    if is_perfect_number(num):\n",
    "        perfect_numbers.append(num)\n",
    "\n",
    "# 打印结果\n",
    "print('1000以内的完数有：', perfect_numbers)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 10.有一分数序列：2/1，3/2，5/3，8/5，13/8，21/13...求出这个数列的前N项之和，N由键盘输入。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数列的前 100 项之和是：162.1029798996649\n"
     ]
    }
   ],
   "source": [
    "# 获取用户输入的项数 N\n",
    "N = int(input('请输入项数 N：'))\n",
    "\n",
    "# 初始化分子和分母\n",
    "numerator = 2\n",
    "denominator = 1\n",
    "\n",
    "# 初始化前N项之和\n",
    "sum_series = 0\n",
    "\n",
    "# 计算前N项之和\n",
    "for _ in range(N):\n",
    "    sum_series += numerator / denominator\n",
    "    # 更新分子和分母\n",
    "    numerator, denominator = numerator + denominator, numerator\n",
    "\n",
    "# 打印结果\n",
    "print(f'数列的前 {N} 项之和是：{sum_series}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 11.输入任意数N，求1+2!+3!+...+N!的值（注意实数范围）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1次计算的结果为:sum=1\n",
      "第2次计算的结果为:sum=3\n",
      "第3次计算的结果为:sum=9\n",
      "第4次计算的结果为:sum=33\n",
      "第5次计算的结果为:sum=153\n",
      "第6次计算的结果为:sum=873\n",
      "第7次计算的结果为:sum=5913\n",
      "第8次计算的结果为:sum=46233\n",
      "第9次计算的结果为:sum=409113\n",
      "第10次计算的结果为:sum=4037913\n",
      "第11次计算的结果为:sum=43954713\n",
      "第12次计算的结果为:sum=522956313\n",
      "第13次计算的结果为:sum=6749977113\n",
      "第14次计算的结果为:sum=93928268313\n",
      "第15次计算的结果为:sum=1401602636313\n",
      "第16次计算的结果为:sum=22324392524313\n",
      "第17次计算的结果为:sum=378011820620313\n",
      "第18次计算的结果为:sum=6780385526348313\n",
      "第19次计算的结果为:sum=128425485935180313\n",
      "第20次计算的结果为:sum=2561327494111820313\n",
      "第21次计算的结果为:sum=53652269665821260313\n",
      "第22次计算的结果为:sum=1177652997443428940313\n",
      "第23次计算的结果为:sum=27029669736328405580313\n",
      "第24次计算的结果为:sum=647478071469567844940313\n",
      "第25次计算的结果为:sum=16158688114800553828940313\n",
      "第26次计算的结果为:sum=419450149241406189412940313\n",
      "第27次计算的结果为:sum=11308319599659758350180940313\n",
      "第28次计算的结果为:sum=316196664211373618851684940313\n",
      "第29次计算的结果为:sum=9157958657951075573395300940313\n",
      "第30次计算的结果为:sum=274410818470142134209703780940313\n",
      "第31次计算的结果为:sum=8497249472648064951935266660940313\n",
      "第32次计算的结果为:sum=271628086406341595119153278820940313\n",
      "第33次计算的结果为:sum=8954945705218228090637347680100940313\n",
      "第34次计算的结果为:sum=304187744744822368938255957323620940313\n",
      "第35次计算的结果为:sum=10637335711130967298604907294846820940313\n",
      "第36次计算的结果为:sum=382630662501032184766604355445682020940313\n",
      "第37次计算的结果为:sum=14146383753727377231082583937026584420940313\n",
      "第38次计算的结果为:sum=537169001220328488991089808037100875620940313\n",
      "第39次计算的结果为:sum=20935051082417771847631371547939998232420940313\n",
      "第40次计算的结果为:sum=836850334330315506193242641144055892504420940313\n",
      "第41次计算的结果为:sum=34289376947494122614363304694584807557656420940313\n",
      "第42次计算的结果为:sum=1439295494700374021157505910939096377494040420940313\n",
      "第43次计算的结果为:sum=61854558558074209658512637979453093884758552420940313\n",
      "第44次计算的结果为:sum=2720126133346522977702138448994068984204397080420940313\n",
      "第45次计算的结果为:sum=122342346998826717539665299944651784048588130840420940313\n",
      "第46次计算的结果为:sum=5624964506810915667389970728744906677010239883800420940313\n",
      "第47次计算的结果为:sum=264248206017979096310354325882356886646207872272920420940313\n",
      "第48次计算的结果为:sum=12678163798554051767172643373255731925167694226950680420940313\n",
      "第49次计算的结果为:sum=620960027832821612639424806694551108812720525606160920420940313\n",
      "第50次计算的结果为:sum=31035053229546199656252032972759319953190362094566672920420940313\n",
      "\n",
      "\n",
      "最终计算的结果为sum=31035053229546199656252032972759319953190362094566672920420940313\n"
     ]
    }
   ],
   "source": [
    "N = int(input('请输入一个正整数'))\n",
    "i,sum,j=1,0,1\n",
    "for i in range(1,N+1): #阶乘的底数呈递增趋势\n",
    "    j*=i#累乘\n",
    "    sum+=j#阶乘累加求和\n",
    "    print(f'第{i}次计算的结果为:sum={sum}');\n",
    "print('\\n')\n",
    "print(f'最终计算的结果为sum={sum}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 12.一个整数，它加上100后是一个完全平方数，再加上268又是一个完全平方数，请问该数是多少？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该数是：4256\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "# 找出满足条件的整数\n",
    "for num in range(1, 10000):\n",
    "    if math.isqrt(num + 100) ** 2 == num + 100 and math.isqrt(num + 100 + 268) ** 2 == num + 100 + 268:\n",
    "        print(f'该数是：{num}')\n",
    "        break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 13.输入任意奇数N，按如下要求打印出图案（菱形）\n",
    "   *\n",
    "  ***\n",
    " *****\n",
    "*******（N控制行数和本行的星数）\n",
    " *****\n",
    "  ***\n",
    "   *\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  *\n",
      " ***\n",
      "*****\n",
      " ***\n",
      "  *\n"
     ]
    }
   ],
   "source": [
    "# 获取用户输入的奇数 N\n",
    "N = int(input('请输入一个奇数 N：'))\n",
    "\n",
    "# 确保输入的是一个奇数\n",
    "if N % 2 == 0:\n",
    "    print('请输入一个奇数')\n",
    "else:\n",
    "    # 打印上半部分\n",
    "    for i in range(1, N + 1, 2):\n",
    "        print(' ' * ((N - i) // 2) + '*' * i)\n",
    "    \n",
    "    # 打印下半部分\n",
    "    for i in range(N - 2, 0, -2):\n",
    "        print(' ' * ((N - i) // 2) + '*' * i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 14.有一对兔子，从出生后第3个月起每个月都生一对兔子，小兔子长到第三个月后每个月又生一对兔子，假如兔子都不死，问每个月的兔子总数为多少？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1个月的兔子总数为：1\n",
      "第2个月的兔子总数为：1\n",
      "第3个月的兔子总数为：2\n",
      "第4个月的兔子总数为：3\n",
      "第5个月的兔子总数为：5\n",
      "第6个月的兔子总数为：8\n",
      "第7个月的兔子总数为：13\n",
      "第8个月的兔子总数为：21\n",
      "第9个月的兔子总数为：34\n",
      "第10个月的兔子总数为：55\n",
      "第11个月的兔子总数为：89\n",
      "第12个月的兔子总数为：144\n"
     ]
    }
   ],
   "source": [
    "# 获取用户输入的月份数\n",
    "months = int(input('请输入月份数：'))\n",
    "\n",
    "# 初始化斐波那契数列的前两项\n",
    "a, b = 1, 1\n",
    "\n",
    "# 打印前两个月的兔子总数\n",
    "print(f'第1个月的兔子总数为：{a}')\n",
    "print(f'第2个月的兔子总数为：{b}')\n",
    "\n",
    "# 计算并打印每个月的兔子总数\n",
    "for month in range(3, months + 1):\n",
    "    a, b = b, a + b\n",
    "    print(f'第{month}个月的兔子总数为：{b}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 15.对10个数进行排序。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "排序后的结果是： [1, 3, 5, 10, 22, 23, 55, 100, 444, 999]\n"
     ]
    }
   ],
   "source": [
    "# 获取用户输入的10个数\n",
    "numbers = []\n",
    "for i in range(10):\n",
    "    num = int(input(f'请输入第{i+1}个数：'))\n",
    "    numbers.append(num)\n",
    "\n",
    "# 对10个数进行排序\n",
    "sorted_numbers = sorted(numbers)\n",
    "\n",
    "# 打印排序后的结果\n",
    "print('排序后的结果是：', sorted_numbers)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 16.任意输入一个3x3矩阵，求矩阵对角线元素之和。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入3x3矩阵，每行输入3个数字，用空格分隔：\n",
      "矩阵对角线元素之和为：44\n"
     ]
    }
   ],
   "source": [
    "# 初始化3x3矩阵\n",
    "matrix = []\n",
    "\n",
    "# 获取用户输入的3x3矩阵\n",
    "print('请输入3x3矩阵，每行输入3个数字，用空格分隔：')\n",
    "for i in range(3):\n",
    "    row = list(map(int, input(f'请输入第{i+1}行：').split()))\n",
    "    matrix.append(row)\n",
    "\n",
    "# 计算对角线元素之和\n",
    "diagonal_sum = 0\n",
    "for i in range(3):\n",
    "    diagonal_sum += matrix[i][i]  # 主对角线元素\n",
    "    diagonal_sum += matrix[i][2 - i]  # 副对角线元素\n",
    "\n",
    "# 如果矩阵是3x3的，主对角线和副对角线的交点会被计算两次，需要减去一次\n",
    "diagonal_sum -= matrix[1][1]\n",
    "\n",
    "# 打印对角线元素之和\n",
    "print(f'矩阵对角线元素之和为：{diagonal_sum}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 17.有一个已经排好序的数组（元素数量大于10）。从键盘任意输入一个数，要求按原来的规律将它插入数组中。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "插入后的数组是： [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 25]\n"
     ]
    }
   ],
   "source": [
    "# 已经排好序的数组\n",
    "sorted_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]\n",
    "\n",
    "# 获取用户输入的一个数\n",
    "num = int(input('请输入一个数：'))\n",
    "\n",
    "# 找到插入位置并插入\n",
    "for i in range(len(sorted_array)):\n",
    "    if num <= sorted_array[i]:\n",
    "        sorted_array.insert(i, num)\n",
    "        break\n",
    "else:\n",
    "    sorted_array.append(num)\n",
    "\n",
    "# 打印插入后的数组\n",
    "print('插入后的数组是：', sorted_array)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 18.有n个整数，使其前面各数顺序向后移m个位置，最后m个数变成最前面的m个数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "移动后的列表是： [33]\n"
     ]
    }
   ],
   "source": [
    "# 获取用户输入的整数列表\n",
    "nums = list(map(int, input('请输入n个整数，用空格分隔：').split()))\n",
    "\n",
    "# 获取用户输入的移动位置 m\n",
    "m = int(input('请输入向后移动的位置 m：'))\n",
    "\n",
    "# 确保 m 在合理范围内\n",
    "m = m % len(nums)\n",
    "\n",
    "# 将列表前面各数顺序向后移 m 个位置\n",
    "nums = nums[-m:] + nums[:-m]\n",
    "\n",
    "# 打印结果\n",
    "print('移动后的列表是：', nums)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 19.有n个人围成一圈，顺序排号。从第一个人开始报数（从1到3报数），凡报到3的人退出圈子，问最后留下的是原来第几号的那位。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最后留下的是原来第 14 号的那位。\n"
     ]
    }
   ],
   "source": [
    "def josephus(n, k):\n",
    "    # 创建一个包含所有人的列表\n",
    "    people = list(range(1, n + 1))\n",
    "    \n",
    "    # 初始化当前索引\n",
    "    index = 0\n",
    "    \n",
    "    # 循环直到只剩下一个人\n",
    "    while len(people) > 1:\n",
    "        # 计算下一个要退出的人的索引\n",
    "        index = (index + k - 1) % len(people)\n",
    "        # 移除该人\n",
    "        people.pop(index)\n",
    "    \n",
    "    # 返回最后剩下的人的编号\n",
    "    return people[0]\n",
    "\n",
    "# 获取用户输入的总人数 n 和报数的值 k\n",
    "n = int(input('请输入总人数 n：'))\n",
    "k = 3  # 报数的值固定为3\n",
    "\n",
    "# 计算最后留下的人的编号\n",
    "last_person = josephus(n, k)\n",
    "\n",
    "# 打印结果\n",
    "print(f'最后留下的是原来第 {last_person} 号的那位。')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 20.编写一个函数，输入n为偶数时，调用函数求1/2+1/4+...+1/n,当输入n为奇数时，调用函数1/1+1/3+...+1/n(利用指针函数)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "计算结果为：2.947675838573917\n"
     ]
    }
   ],
   "source": [
    "def sum_even(n):\n",
    "    \"\"\"计算1/2 + 1/4 + ... + 1/n\"\"\"\n",
    "    sum = 0\n",
    "    for i in range(2, n + 1, 2):\n",
    "        sum += 1 / i\n",
    "    return sum\n",
    "\n",
    "def sum_odd(n):\n",
    "    \"\"\"计算1/1 + 1/3 + ... + 1/n\"\"\"\n",
    "    sum = 0\n",
    "    for i in range(1, n + 1, 2):\n",
    "        sum += 1 / i\n",
    "    return sum\n",
    "\n",
    "def calculate_sum(n):\n",
    "    \"\"\"根据n的奇偶性调用相应的函数\"\"\"\n",
    "    if n % 2 == 0:\n",
    "        return sum_even(n)\n",
    "    else:\n",
    "        return sum_odd(n)\n",
    "\n",
    "# 获取用户输入的整数 n\n",
    "n = int(input('请输入一个整数 n：'))\n",
    "\n",
    "# 计算并打印结果\n",
    "result = calculate_sum(n)\n",
    "print(f'计算结果为：{result}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
